package com.javaee.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.session.EmployeeManagerRemote;

/**
 * Servlet implementation class EmployeServlet
 */
public class EmployeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    
    @EJB
	EmployeeManagerRemote emp;  
    
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher dispatch = request.getRequestDispatcher("/createemployee.jsp");		
		if (request.getParameterMap().isEmpty() || request.getParameter("action").equals("viewall")) {			
			viewallEmployees(request, response);
		}	
		else if(request.getParameter("action").equals("modif")) {		
			if (request.getParameter("id") != null) // Modification
			{
				int idModif = Integer.parseInt(request.getParameter("id"));
				Employee empModif = new Employee();
				empModif = emp.GetEmployeParId(idModif);
				if (empModif != null) {
					System.out.println(empModif.getUsername());
					request.setAttribute("Employee",empModif);					
				}
			}			
			dispatch.forward(request,response);				
		}
		else if (request.getParameter("action").equals("save")) {
			Employee empSave = new Employee();
			ArrayList <String> errors = new ArrayList<String>();
			if (request.getParameter("username").equals(""))
				errors.add("Le champ username ne doit pas être nul");
			empSave.setUsername(request.getParameter("username"));
			if (request.getParameter("username").equals(request.getParameter("password"))) {
				if (request.getParameter("password").equals(""))								
					errors.add("Le champ password ne doit pas être nul");
				else
					errors.add("L'username et le mot de passe ne peuvent pas être identiques");
			}
			empSave.setPassword(request.getParameter("password"));
			empSave.setFirstname(request.getParameter("firstname"));
			empSave.setLastname(request.getParameter("lastname"));
			empSave.setEmail(request.getParameter("email"));			
			if (request.getParameter("birthdate").equals("") )
				errors.add("Le champ birthdate ne doit pas être nul");
			else
			{
				Date birthdate = formatDate(request.getParameter("birthdate"));
				empSave.setBirthdate(birthdate);
			}
			if (request.getParameter("indate").equals(""))
				errors.add("Le champ indate ne doit pas être nul");
			else
			{
				Date indate = formatDate(request.getParameter("indate"));
				empSave.setIndate(indate);
			}
			if (errors.isEmpty()) {
				if (Integer.parseInt(request.getParameter("id")) == 0) {
					emp.AjouterEmploye(empSave);
				}
				else
				{
					empSave.setIdemployee(Integer.parseInt(request.getParameter("id")));
					emp.UpdateEmploye(empSave);				
				}
				viewallEmployees(request, response);
			}
			else
			{
				request.setAttribute("errors", errors);
				request.setAttribute("Employee", empSave);
				dispatch.forward(request,response);
			}			
		}
		else if (request.getParameter("action").equals("delete")) {
			int idSuppr = Integer.parseInt(request.getParameter("id"));					
			emp.DeleteEmploye(idSuppr);
			viewallEmployees(request, response);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public void viewallEmployees(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatchAll = request.getRequestDispatcher("/viewallEmployees.jsp");
		List<Employee> employees = emp.GetEmployes();
		request.setAttribute("listEmployees", employees);			
		dispatchAll.forward(request,response);
	}
	
	public Date formatDate(String dateToFormat){		
		Calendar cdateToFormat = Calendar.getInstance();
		cdateToFormat.set(Integer.parseInt(dateToFormat.substring(6,10)),
				Integer.parseInt(dateToFormat.substring(3,5)),
				Integer.parseInt(dateToFormat.substring(0,2))
		);
		Date dateFormated = cdateToFormat.getTime();
		return dateFormated;
	}

}
